home *** CD-ROM | disk | FTP | other *** search
/ Video Toaster 4.2 / Video Toaster v4.2.iso / programs / documentation / lightwave / sdk / sample / spherize / spherize.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-23  |  3.5 KB  |  158 lines

  1. /*
  2.  * SPHERIZE.C -- Modeler Plugin Mesh Edit
  3.  *         Conform selected points to unit sphere
  4.  *
  5.  * written by Stuart Ferguson
  6.  * last revision  12/6/94
  7.  */
  8. #include <splug.h>
  9. #include <lwmod.h>
  10. #include <string.h>
  11. #include <math.h>
  12.  
  13.  
  14. /*
  15.  * Local information packet.  This includes the mesh edit context and the
  16.  * monitor, if any.
  17.  */
  18. typedef struct st_MyData {
  19.     MeshEditOp        *op;
  20.     Monitor            *mon;
  21. } MyData;
  22.  
  23.  
  24. /*
  25.  * Point scan callback for spherizing.  This is called for every point in
  26.  * the primary layer dataset, so we must filter for selection mode.
  27.  */
  28.     static EDError
  29. Spherize (
  30.     MyData            *dat,
  31.     const PointInfo        *info)
  32. {
  33.     MeshEditOp        *op = dat->op;
  34.     double             new[3];
  35.     double             d;
  36.     int             i;
  37.  
  38.     /*
  39.      * Skip this point if it is not selected.
  40.      */
  41.     if (!(info->flags & PPDF_SELECT))
  42.         return EDERR_NONE;
  43.  
  44.     /*
  45.      * Count this selected point in the aggregate for the monitor.  The
  46.      * step function returns True if the user has requested an abort,
  47.      * which we can propogate by returning the appropriate code.
  48.      */
  49.     if (dat->mon && (*dat->mon->step) (dat->mon->data, 1))
  50.         return EDERR_USERABORT;
  51.  
  52.     /*
  53.      * Compute distance of point to origin.  If zero, skip this point.
  54.      */
  55.     d = 0.0;
  56.     for (i = 0; i < 3; i++)
  57.         d += info->position[i] * info->position[i];
  58.  
  59.     d = sqrt (d);
  60.     if (d <= 0.0)
  61.         return EDERR_NONE;
  62.  
  63.     /*
  64.      * Compute normalized coordinates for the point and attempt to
  65.      * move it.  Any error that might result will halt the scan and
  66.      * be returned to the activation function.
  67.      */
  68.     for (i = 0; i < 3; i++)
  69.         new[i] = info->position[i] / d;
  70.  
  71.     return ((*op->pntMove) (op->state, info->pnt, new));
  72. }
  73.  
  74.  
  75. /*
  76.  * Entry point for mesh edit plugin.
  77.  */
  78.     XCALL_(int)
  79. Activate (
  80.     long             version,
  81.     GlobalFunc        *global,
  82.     MeshEditBegin        *local,
  83.     void            *serverData)
  84. {
  85.     MeshEditOp        *op;
  86.     DynaMonitorFuncs    *mfunc;
  87.     MyData             dat;
  88.     unsigned int         count;
  89.     EDError             err;
  90.  
  91.     /*
  92.      * Check interface version.
  93.      */
  94.     XCALL_INIT;
  95.     if (version != 1)
  96.         return AFUNC_BADVERSION;
  97.  
  98.     /*
  99.      * Get global data -- monitor functions.
  100.      */
  101.     mfunc = (*global) ("LWM: Dynamic Monitor", GFUSE_TRANSIENT);
  102.     if (!mfunc)
  103.         return AFUNC_BADGLOBAL;
  104.  
  105.     /*
  106.      * Attempt to begin edit operation.  If the startup fails we still
  107.      * return OK, but just don't do anything.
  108.      */
  109.     op = (*local) (0, 0, OPSEL_USER);
  110.     if (!op)
  111.         return AFUNC_OK;
  112.  
  113.     /*
  114.      * Start the monitor.  If we can create a monitor, count the points
  115.      * that will be affected by the operation and init the monitor with
  116.      * that value.
  117.      */
  118.     dat.mon = (*mfunc->create) ("Spherize", NULL);
  119.     if (dat.mon) {
  120.         count = (*op->pointCount) (op->state, OPLYR_PRIMARY,
  121.                        EDCOUNT_SELECT);
  122.         (*dat.mon->init) (dat.mon->data, count);
  123.     }
  124.  
  125.     /*
  126.      * Scan points and apply transformation.  The `scan' function will
  127.      * traverse the database of points for the primary edit layer and
  128.      * call the client's function with the client's data pointer and
  129.      * info for the point.  Errors (or any non-zero value) returned by
  130.      * the client's function will be returned here.  If the scan
  131.      * completes, EDERR_NONE is returned.
  132.      */
  133.     dat.op = op;
  134.     err = (*op->pointScan) (op->state, Spherize, &dat, OPLYR_PRIMARY);
  135.  
  136.     /*
  137.      * End the monitor whether we completed the process or not.
  138.      */
  139.     if (dat.mon) {
  140.         (*dat.mon->done) (dat.mon->data);
  141.         (*mfunc->destroy) (dat.mon);
  142.     }
  143.  
  144.     /*
  145.      * Complete the operation by calling `done.'  We will pass an error
  146.      * code if there was one.
  147.      */
  148.     (*op->done) (op->state, err, 0);
  149.     return AFUNC_OK;
  150. }
  151.  
  152.  
  153. /*
  154.  * Globals necessary to declare the class and name of this plugin server.
  155.  */
  156. char        ServerClass[] = "MeshDataEdit";
  157. char        ServerName[]  = "Demo_Spherize";
  158.